﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using DevExpress.Xpo;
using DevExpress.Data.Filtering;

namespace POI.eTransmittal.Enitity
{
    [OptimisticLocking(false),DeferredDeletion(false)]
    public class IncomingTransmittal : XPCustomObject
    {
        public const string STATUS_DRAFT="Draft";
        public const string STATUS_DISTRIBUTED="Distributed";
        public string FaxFileLink;
      
        public string Status;
        #region Fax Properties
        public string Subject;
        public string  From;
     
        public string FaxFrom;
     
        public DateTime ReceivedDate;
        public string InboxName;
        public string Remarks;
        public string OriginalFileName;
        #endregion

        #region Distribution
        [Key(true)]
        public int ID;
        public string Attn;
        public string Cc;

        public string AttnNames
        {
            get
            {
                string[] userIDs = Attn.Split(';');
                string userNames = "";
                
                foreach (string userId in userIDs)
                {
                    TransmittalUser user = Session.FindObject<TransmittalUser>(CriteriaOperator.Parse("UserID=?", userId));

                    if (user != null)
                    {
                        if (userNames != "")
                            userNames += ", ";

                        userNames += user.Name;
                    }
                }

                return userNames;
            }
        }
        public string CcNames
        {
            get
            {
                string[] userIDs = Cc.Split(';');
                string userNames = "";

                foreach (string userId in userIDs)
                {
                    TransmittalUser user = Session.FindObject<TransmittalUser>(CriteriaOperator.Parse("UserID=?", userId));

                    if (user != null)
                    {
                        if (userNames != "")
                            userNames += ", ";

                        userNames += user.Name;
                    }
                }

                return userNames;
            }
        }

        public string UncLink
        {
            get
            {
                return GetUNC_FileLink();
            }
        }
        #endregion
        
        #region AuditTrail

        public DateTime CreatedDate;

        public string CreatedBy;

        public DateTime LastChangedDate;

        public string LastChangedBy;

        public void Save(string userId)
        {
            if (CreatedBy == null || CreatedDate == DateTime.MinValue)
            {
                this.CreatedDate = DateTime.Now;
                this.CreatedBy = userId;
            }

            this.LastChangedDate = DateTime.Now;
            this.LastChangedBy = userId;

            this.Save();
        }

        #endregion
        public IncomingTransmittal()
            : base()
        {
            // This constructor is used when an object is loaded from a persistent storage.
            // Do not place any code here.
        }

        public IncomingTransmittal(Session session)
            : base(session)
        {
            // This constructor is used when an object is loaded from a persistent storage.
            // Do not place any code here.
        }


        public string GetFileName(string ext)
        {
            string first = "TI";
            string second = CreatedDate.ToString("yyMM");
            string three = ID.ToString();
            return string.Format("{0}-{1}-{2}{3}", first, second, three, ext);
        }

        public string GetUNC_FileLink()
        {
            if (!string.IsNullOrEmpty(FaxFileLink))
            {
                string uncPath = FaxFileLink.Replace("\\", "/");
                return "file://" + uncPath;
            }
            return null;
        }

        public string GetAttnEmailAddress()
        {
         return    quickResolve(Attn);
           
        }

        private string quickResolve(string texts)
        {
            string[] splits = texts.Split(';');
            StringBuilder sb = new StringBuilder();
            bool first = true;
            foreach (string sp in splits)
            {
                if (!string.IsNullOrEmpty(sp))
                {
                    if (first)
                    {
                        sb.Append(sp + "@premier-oil.com");
                        first = false;
                    }
                    else
                    {
                        sb.Append(";" + sp + "@premier-oil.com");
                    }
                }
            }
            return sb.ToString();
        }

        public string GetCcEmailAddress()
        {
            return quickResolve(Cc);
        }
    }
}
